package com.qiqi.web.man;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qiqi.dao.PhoneNumberMapper;
import com.qiqi.domain.PhoneNumber;
import com.qiqi.domain.PhoneNumberExample;
import com.qiqi.domain.PhoneNumberExample.Criteria;
import com.qiqi.service.phonenumber.PhoneNumberService;

/**
 * 号码资源管理
 * 
 * @author wangyoujin
 * 
 */
@Controller
public class NumberController {
	@Autowired
	private PhoneNumberService phoneNumberService;
	@Autowired
	private PhoneNumberMapper phoneNumberMapper;

	@RequestMapping("/man/num")
	public String toList() {
		return "/man/number/list";
	}

	@RequestMapping("/man/num/find")
	@ResponseBody
	public Map<String, Object> list(ModelMap model, String num, Integer fee,
			int page, int pageSize) {
		PhoneNumberExample query = new PhoneNumberExample();
		Criteria criteria = query.createCriteria();
		if (StringUtils.isNotEmpty(num)) {
			criteria.andNumLike("%"+num+"%");
		}
		if (fee!=null) {
			criteria.andPrepayEqualTo(fee);
		}
		query.setOrderByClause("createTime desc");
		Map<String, Object> res = phoneNumberService.findNumbersByPage(query,
				page, pageSize);
		return res;
	}

	@RequestMapping("/man/num/update")
	@ResponseBody
	public Map<String, Object> update(PhoneNumber number) {
		number.setCreatetime(new Date());
		int state=0;
		if (number.getId()!=null&&number.getId()!=0) {
			state = phoneNumberMapper.updateByPrimaryKeySelective(number);
		}else{
			//add
			number.setSelled(0);
			state = phoneNumberService.addNumber(number);
		}
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("success", state > 0);
		return res;
	}

	@RequestMapping("/man/num/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable Integer id) {
		int state = phoneNumberMapper.deleteByPrimaryKey(id);
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("success", state > 0);
		return res;
	}
	/**
	 * 批量更新号码资源
	 * @param nums
	 * @param fee
	 * @return
	 */
	@RequestMapping("/man/num/batchupdate")
	@ResponseBody
	public Map<String,Object> updateBatch(String nums,Integer fee){
		Map<String, Object> res = new HashMap<String, Object>();
		int ct=0;
		String[] ns=nums.split(",");
		PhoneNumber number=null;
		for(String num:ns){
			number = new PhoneNumber();
			number.setCreatetime(new Date());
			number.setSelled(0);
			number.setPrepay(fee);
			number.setNum(num);
			int success=phoneNumberService.addNumber(number);
			PhoneNumberExample count = new PhoneNumberExample();
			count.createCriteria().andNumEqualTo(num);
			if (success > 0) {
				ct++;
			}
		}
		res.put("success", true);
		res.put("total", ct);
		return res;
	}
	
}
